package cc.comac.mom.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipRepairDocService;
import cc.comac.mom.bean.EquipDev;
import cc.comac.mom.bean.EquipRepair;
import cc.comac.mom.bean.EquipRepairDoc;
import cc.comac.mom.bean.EquipRepairRepositoryRepairDoc;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.datahandlers.entity.EquipDevDataHandler;
import cc.comac.mom.datahandlers.entity.EquipRepairDocDataHandler;
import cc.comac.mom.datahandlers.entity.EquipRepairRepositoryRepairDocDataHandler;
import cc.comac.mom.entity.EquipRepairDocEntity;
import cc.comac.mom.util.ResultBean;

@Service
@RestController
public class EquipRepairDocServiceImpl implements EquipRepairDocService{

	@Autowired
	private EquipRepairDocDataHandler equipRepairDocDataHandler;
	
	@Autowired
	private EquipDevDataHandler equipDevDataHandler;
	
	@Autowired
	private EquipRepairRepositoryRepairDocDataHandler EquipRepairRepositoryRepairDocDataHandler;
	
	
	@Override
	public ResultBean insertBatch(@RequestBody List<EquipRepairDoc> list) {
		// TODO Auto-generated method stub
		return equipRepairDocDataHandler.insertBatch(list);
	}

	@Override
	public ResultBean delete(@RequestBody EquipRepairDoc obj) {
		// TODO Auto-generated method stub
		return equipRepairDocDataHandler.delete(obj);
	}

	@Override
	public ResultBean selectByCondition(@RequestBody EquipRepairDoc obj) {
		ResultBean resultBean = new ResultBean(true);
		LimitPageBean<EquipRepairDocEntity> selectByCondition = equipRepairDocDataHandler.selectByCondition(obj);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}

	@Override
	public ResultBean getRepqirRepositoryDocInfo(@RequestBody EquipRepair obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			return null;
		}
		long faultCauseId = obj.getInnerData().getFaultCauseId();
		if (faultCauseId == 0) {
			resultBean.setErrorMessge("请先进行维修分析");
			return resultBean;
		}
		EquipDev equipDev = equipDevDataHandler.getById(obj.getInnerData().getDevId());
		if (equipDev == null) {
			resultBean.setErrorMessge("无法获取设备主数据信息");
			return resultBean;
		}
		EquipRepairRepositoryRepairDoc equipRepairRepositoryRepairDoc = new EquipRepairRepositoryRepairDoc();
		equipRepairRepositoryRepairDoc.setEquipCategoryId(equipDev.getInnerData().getDevCategoryId());
		equipRepairRepositoryRepairDoc.setFaultCauseId(obj.getInnerData().getFaultCauseId());
		equipRepairRepositoryRepairDoc.setPageNum(obj.getPageNum());
		equipRepairRepositoryRepairDoc.setPageSize(obj.getPageSize());
		ResultDev selectByCondition = EquipRepairRepositoryRepairDocDataHandler.selectByCondition(equipRepairRepositoryRepairDoc);
		resultBean.setObject(selectByCondition);
		return resultBean;
	}

	@Override
	public byte[] previewPdf(@RequestParam long documentId) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (documentId == 0L) {
			throw new RuntimeException("当前未选中数据");
		}
		EquipRepairDocEntity byId = equipRepairDocDataHandler.getById(documentId);
		File file = new File(byId.getDoucumentRealPath());
		byte[] data = null;
		if (file.exists()) {

			try {
				FileInputStream input = new FileInputStream(file);
				data = new byte[input.available()];
				input.read(data);
				input.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			throw new RuntimeException("无法找到当前文件路径");
		}
		return data;
	}

	@Override
	public EquipRepairDocEntity selectByDocumentId(Long documentId) {
		EquipRepairDocEntity byId = equipRepairDocDataHandler.getById(documentId);
		return byId;
	}
}
